package cn.bonoon.kernel.io;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bonoon.kernel.util.StringHelper;

/**
 * 描述一个文件的基本信息；
 * 这个对象是来自于http请求的文件
 * 
 * @author jackson
 *
 */
public class HttpStorageFile extends AbstractBFile{

	public static final String DEFAULT_CONTENT_TYPE = "multipart/form-data";
	
	public static final Map<String, String> CONTENT_TYPES = new HashMap<String, String>(){
		
		private static final long serialVersionUID = -1543532151027171596L;

		/*
ContentTypes : "ez","application/andrew-inset" 
ContentTypes : "hqx","application/mac-binhex40" 
ContentTypes : "cpt","application/mac-compactpro" 
ContentTypes : "doc","application/msword" 
ContentTypes : "bin","application/octet-stream" 
ContentTypes : "dms","application/octet-stream" 
ContentTypes : "lha","application/octet-stream" 
ContentTypes : "lzh","application/octet-stream" 
ContentTypes : "exe","application/octet-stream" 
ContentTypes : "class","application/octet-stream" 
ContentTypes : "so","application/octet-stream" 
ContentTypes : "dll","application/octet-stream" 
ContentTypes : "oda","application/oda" 
ContentTypes : "pdf","application/pdf" 
ContentTypes : "ai","application/postscript" 
ContentTypes : "eps","application/postscript" 
ContentTypes : "ps","application/postscript" 
ContentTypes : "smi","application/smil" 
ContentTypes : "smil","application/smil" 
ContentTypes : "mif","application/vnd.mif" 
ContentTypes : "xls","application/vnd.ms-excel" 
ContentTypes : "ppt","application/vnd.ms-powerpoint" 
ContentTypes : "wbxml","application/vnd.wap.wbxml" 
ContentTypes : "wmlc","application/vnd.wap.wmlc" 
ContentTypes : "wmlsc","application/vnd.wap.wmlscriptc" 
ContentTypes : "bcpio","application/x-bcpio" 
ContentTypes : "vcd","application/x-cdlink" 
ContentTypes : "pgn","application/x-chess-pgn" 
ContentTypes : "cpio","application/x-cpio" 
ContentTypes : "csh","application/x-csh" 
ContentTypes : "dcr","application/x-director" 
ContentTypes : "dir","application/x-director" 
ContentTypes : "dxr","application/x-director" 
ContentTypes : "dvi","application/x-dvi" 
ContentTypes : "spl","application/x-futuresplash" 
ContentTypes : "gtar","application/x-gtar" 
ContentTypes : "hdf","application/x-hdf" 
ContentTypes : "js","application/x-javascript" 
ContentTypes : "skp","application/x-koan" 
ContentTypes : "skd","application/x-koan" 
ContentTypes : "skt","application/x-koan" 
ContentTypes : "skm","application/x-koan" 
ContentTypes : "latex","application/x-latex" 
ContentTypes : "nc","application/x-netcdf" 
ContentTypes : "cdf","application/x-netcdf" 
ContentTypes : "sh","application/x-sh" 
ContentTypes : "shar","application/x-shar" 
ContentTypes : "swf","application/x-shockwave-flash" 
ContentTypes : "sit","application/x-stuffit" 
ContentTypes : "sv4cpio","application/x-sv4cpio" 
ContentTypes : "sv4crc","application/x-sv4crc" 
ContentTypes : "tar","application/x-tar" 
ContentTypes : "tcl","application/x-tcl" 
ContentTypes : "tex","application/x-tex" 
ContentTypes : "texinfo","application/x-texinfo" 
ContentTypes : "texi","application/x-texinfo" 
ContentTypes : "t","application/x-troff" 
ContentTypes : "tr","application/x-troff" 
ContentTypes : "roff","application/x-troff" 
ContentTypes : "man","application/x-troff-man" 
ContentTypes : "me","application/x-troff-me" 
ContentTypes : "ms","application/x-troff-ms" 
ContentTypes : "ustar","application/x-ustar" 
ContentTypes : "src","application/x-wais-source" 
ContentTypes : "xhtml","application/xhtml+xml" 
ContentTypes : "xht","application/xhtml+xml" 
ContentTypes : "zip","application/zip" 
ContentTypes : "au","audio/basic" 
ContentTypes : "snd","audio/basic" 
ContentTypes : "mid","audio/midi" 
ContentTypes : "midi","audio/midi" 
ContentTypes : "kar","audio/midi" 
ContentTypes : "mpga","audio/mpeg" 
ContentTypes : "mp2","audio/mpeg" 
ContentTypes : "mp3","audio/mpeg" 
ContentTypes : "aif","audio/x-aiff" 
ContentTypes : "aiff","audio/x-aiff" 
ContentTypes : "aifc","audio/x-aiff" 
ContentTypes : "m3u","audio/x-mpegurl" 
ContentTypes : "ram","audio/x-pn-realaudio" 
ContentTypes : "rm","audio/x-pn-realaudio" 
ContentTypes : "rpm","audio/x-pn-realaudio-plugin" 
ContentTypes : "ra","audio/x-realaudio" 
ContentTypes : "wav","audio/x-wav" 
ContentTypes : "pdb","chemical/x-pdb" 
ContentTypes : "xyz","chemical/x-xyz" 
ContentTypes : "bmp","image/bmp" 
ContentTypes : "gif","image/gif" 
ContentTypes : "ief","image/ief" 
ContentTypes : "jpeg","image/jpeg" 
ContentTypes : "jpg","image/jpeg" 
ContentTypes : "jpe","image/jpeg" 
ContentTypes : "png","image/png" 
ContentTypes : "tiff","image/tiff" 
ContentTypes : "tif","image/tiff" 
ContentTypes : "djvu","image/vnd.djvu" 
ContentTypes : "djv","image/vnd.djvu" 
ContentTypes : "wbmp","image/vnd.wap.wbmp" 
ContentTypes : "ras","image/x-cmu-raster" 
ContentTypes : "pnm","image/x-portable-anymap" 
ContentTypes : "pbm","image/x-portable-bitmap" 
ContentTypes : "pgm","image/x-portable-graymap" 
ContentTypes : "ppm","image/x-portable-pixmap" 
ContentTypes : "rgb","image/x-rgb" 
ContentTypes : "xbm","image/x-xbitmap" 
ContentTypes : "xpm","image/x-xpixmap" 
ContentTypes : "xwd","image/x-xwindowdump" 
ContentTypes : "igs","model/iges" 
ContentTypes : "iges","model/iges" 
ContentTypes : "msh","model/mesh" 
ContentTypes : "mesh","model/mesh" 
ContentTypes : "silo","model/mesh" 
ContentTypes : "wrl","model/vrml" 
ContentTypes : "vrml","model/vrml" 
ContentTypes : "css","text/css" 
ContentTypes : "html","text/html" 
ContentTypes : "htm","text/html" 
ContentTypes : "asc","text/plain" 
ContentTypes : "txt","text/plain" 
ContentTypes : "rtx","text/richtext" 
ContentTypes : "rtf","text/rtf" 
ContentTypes : "sgml","text/sgml" 
ContentTypes : "sgm","text/sgml" 
ContentTypes : "tsv","text/tab-separated-values" 
ContentTypes : "wml","text/vnd.wap.wml" 
ContentTypes : "wmls","text/vnd.wap.wmlscript" 
ContentTypes : "etx","text/x-setext" 
ContentTypes : "xsl","text/xml" 
ContentTypes : "xml","text/xml" 
ContentTypes : "mpeg","video/mpeg" 
ContentTypes : "mpg","video/mpeg" 
ContentTypes : "mpe","video/mpeg" 
ContentTypes : "qt","video/quicktime" 
ContentTypes : "mov","video/quicktime" 
ContentTypes : "mxu","video/vnd.mpegurl" 
ContentTypes : "avi","video/x-msvideo" 
ContentTypes : "movie","video/x-sgi-movie" 
ContentTypes : "ice","x-conference/x-cooltalk" 
ContentTypes : "form","application/x-www-form-urlencoded" 
		 */
		{ 
			put(".doc", "application/msword");
			put(".docx", "application/msword");
			put(".pdf", "application/pdf");
			put(".xls", "application/vnd.ms-excel");
			put(".xlsx", "application/vnd.ms-excel");
			put(".ppt", "application/vnd.ms-powerpoint");
			put(".pptx", "application/vnd.ms-powerpoint");
			put(".swf", "application/x-shockwave-flash");
			put(".zip", "application/zip");
			
			put(".mpga", "audio/mpeg");
			put(".mp2", "audio/mpeg");
			put(".mp3", "audio/mpeg");
			put(".ram", "audio/x-pn-realaudio");
			put(".rm", "audio/x-pn-realaudio");
			put(".wav", "audio/x-wav");
			
			put(".bmp", "image/bmp");
			put(".gif", "image/gif");
			put(".jpeg", "image/jpeg");
			put(".jpg", "image/jpeg");
			put(".jpe", "image/jpeg");
			put(".png", "image/png");
			put(".tiff", "image/tiff");
			put(".tif", "image/tiff");
			
			put(".txt", "text/plain");
			put(".css", "text/css");
			put(".html", "text/html");
			put(".htm", "text/html");
			put(".xsl", "text/xsl");
			put(".xml", "text/xml");
			
			put(".mpeg", "video/mpeg");
			put(".mpg", "video/mpeg");
			put(".mpe", "video/mpeg");
			put(".mov", "video/quicktime");
			put(".avi", "video/x-msvideo");
			put(".movie", "video/x-sgi-movie");
			put(".flv", "video/x-flv");
			put(".mkv", "video/x-matroska");
		}
	};

	private String contentType;
	private final HttpServletRequest request;
	private final HttpServletResponse response;
	
	private HttpLoadingType loadingType = HttpLoadingType.OPEN;

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}
	
	public HttpStorageFile(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
	}
	
	public HttpStorageFile(HttpStorageFile copy){
		
		this.filename = copy.filename;
		this.filesuffix = copy.filesuffix;
		this.sourcePath = copy.sourcePath;
		
		this.request = copy.request;
		this.response = copy.response;
		this.contentType = copy.contentType;
		
	}

	public HttpStorageFile(HttpServletRequest request, HttpServletResponse response, BFile bfile){
		super(bfile);

		this.request = request;
		this.response = response;
		this.contentType = parseContentType();
		
	}
	
	public HttpStorageFile(HttpServletRequest request, HttpServletResponse response, String fullPath){
		super(fullPath);
		this.request = request;
		this.response = response;
		this.contentType = parseContentType();
	}
	
	public HttpStorageFile(HttpServletRequest request, HttpServletResponse response, String path, String name, String ext){
		super(path, name, ext);
		this.request = request;
		this.response = response;
		this.contentType = parseContentType();
	}

	private String parseContentType(){
		String suffix = interanlResolveFilesuffix();
		String cType = null;
		if(null != suffix){
			cType = CONTENT_TYPES.get(suffix.toLowerCase());
		}
		return StringHelper.get(cType, DEFAULT_CONTENT_TYPE);
	}

	public HttpLoadingType getLoadingType() {
		return loadingType;
	}

	public void setLoadingType(HttpLoadingType loadingType) {
		this.loadingType = loadingType;
	}
	
	public OutputStream getOutputStream() throws Exception{
		loadingType.loading(response, this);
		return response.getOutputStream();
	}
	
	public String getParameter(String name){
		return request.getParameter(name);
	}
	
	public HttpServletResponse getResponse() {
		return response;
	}
}
